<template>
  <el-dialog :title="isEdit?'编辑调整库存':'调整库存'"
             :visible.sync="dialogForm"
             width="1200px">
    <el-form :inline="true"
             ref="form"
             :model="form"
             :rules="rules"
             class="add-form"
             label-width="110px">
      <el-row>
        <el-col class="el-height"><span class="labels">货主单位:</span>{{form.cargoOwnerName}}</el-col>
      </el-row>
      <h3>货物信息</h3>
      <hr />
      <el-row>
        <el-col :span="8"
                class="el-height"><span class="labels">品名:</span>{{form.cargoName}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">规格:</span>{{form.normStr}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">跟踪号:</span>{{form.trackingNo}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">捆包号:</span>{{form.baleNo}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">材质:</span>{{form.material}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">生产厂家:</span>{{form.madeBy}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">计量方式:</span>{{form.measureWay | _filterMeterStyle}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">车船号:</span>{{form.transhipNo}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">炉批号:</span>{{form.stoveNo}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">验收件数:</span>{{form.inventoryNum }}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">验收重量:</span>{{form.inventoryWeight }}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">验收数量:</span>{{form.inventoryCount}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">管理方式:</span>{{form.manageWay | _filterManagement}}</el-col>
      </el-row>
      <h3>库位信息</h3>
      <hr />
      <el-row>
        <el-col :span="8"
                class="el-height"><span class="labels">仓库:</span>{{form.warehouseName}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">库房:</span>{{form.roomName}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">库位:</span>{{form.roomLocation}}</el-col>
      </el-row>
      <h3>调整数量</h3>
      <hr />
      <el-row class="adjustNumber">
        <el-radio-group v-model="form.adjustWay">
          <el-radio :label="'TZRK'">调整入库</el-radio>
          <el-radio :label="'TZCK'">调整出库</el-radio>
        </el-radio-group>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="调整件数"
                        prop="num">
            <el-input type="text"
                      v-model.trim="form.num"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="调整重量"
                        prop="weight">
            <el-input type="text"
                      v-model.trim="form.weight"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="调整数量"
                        prop="countNum">
            <el-input type="text"
                      v-model.trim="form.countNum"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调整原因"
                        prop="adjustDesc">
            <el-input type="text"
                      v-model="form.adjustDesc"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button @click="handleSave"
                 type="primary">保存</el-button>
      <el-button @click="handleSubmit"
                 type="primary">提交</el-button>
      <el-button @click="dialogForm = false">返 回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { adjustStock } from '@/api/matter/stockManage'
import { updateAuditMatter } from '@/api/matter/auditMatter'
import { PUBLIC_INT_NULL_ZERO,PUBLIC_NUMBER } from '@/utils/Reg'
import { twoLength,maxLength } from '@/utils/rules'

export default {
  name: 'adjustStockForm',
  data() {
    return {
      form: this.clearForm(),
      rules: {
        num: [{ validator: PUBLIC_INT_NULL_ZERO, trigger: 'blur' }],
        weight: [{ validator: PUBLIC_NUMBER, trigger: 'blur' }],
        countNum: [{ validator: PUBLIC_INT_NULL_ZERO, trigger: 'blur' }],
        adjustDesc: [maxLength]
      },
      dialogForm: false,
      isEdit: false,
      radio: 1,
      data: []
    }
  },
  methods: {
    clearForm() {
      return {
        adjustDesc: '',
        adjustWay: '',
        countNum: null,
        num: null,
        submitType: null,
        weight: null
      }
    },

    //保存
    handleSave() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.handleSaveEdit() : this.handleSaveAdd()
        } else {
          this.$message({
            message: '请检查表单',
            type: 'error'
          })
        }
      })
    },
    //保存：编辑调整库存
    handleSaveEdit() {
      this.form.submitType = 2
      updateAuditMatter(this.form.id, this.form).then(res => {
        this.$message({ message: '编辑成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    //保存：调整库存
    handleSaveAdd() {
      console.log('this.form',this.form)
      let id
      if(this.form.id){
        id = this.form.id
      } else {
        id = this.form.inventoryId
      }
      this.form.submitType = 2
      adjustStock(id, this.form).then(res => {
        this.$message({ message: '新增成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    //提交
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.handleSubmitEdit() : this.handleSubmitAdd()
        } else {
          this.$message({
            message: '请检查表单',
            type: 'error'
          })
        }
      })
    },
    //提交：编辑调整库存
    handleSubmitEdit() {
      this.form.submitType = 1
      updateAuditMatter(this.form.id, this.form).then(res => {
        this.$message({ message: '提交成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    //提交：调整库存
    handleSubmitAdd() {
      let id
      if(this.form.id){
        id = this.form.id
      } else {
        id = this.form.inventoryId
      }
      this.form.submitType = 1
      adjustStock(id, this.form).then(res => {
        this.$message({ message: '提交成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-height {
  margin: 10px 0;
}
h3 {
  margin: 10px 0;
}
.labels {
  margin-right: 5px;
}
.adjustNumber {
  margin: 20px 0;
  margin-left: 40px;
}
</style>
